package storage;
import java.io.*;
import java.net.*;
import common.*;

public class MeServer{
/*	public static final String SIMUL_SET = "SIMULSET";
	public static final String SIMUL_SET_OK = "SIMULSETOK";
	public static final String SIMUL_READY = "SIMULREADY";
	public static final String PARAM_TOKEN = "TOKEN";
	public static final String PARAM_TOKENLESS = "NOTOKEN";
	
	private BufferedReader buffer_rd;
	private BufferedWriter buffer_wr;
	private InputStream in_stream;
	private OutputStream out_stream;
	private ServerSocket socket;
	
	private String ME_SCHEME;
	private int N_LOCAL_NODES;
	
	
	public MeServer( int new_port ){
		try{
			socket = new ServerSocket( new_port );
		}catch( IOException e ){
			e.printStackTrace();
			System.exit( 0 );
		}
		
		while( true ){
			try{
				System.out.print( "Server stands by... (Press Ctrl-C to break)" );	// blocked waiting
				
				// establish a connection
				Socket tcpSocket = socket.accept();	// convert the connection to a communication socket
				System.out.print( "\nClient, "+ tcpSocket.getInetAddress().getHostAddress() + ", is connected." );
				
				// create read-write buffer (ACSII)
				in_stream = tcpSocket.getInputStream();	// read from the socket
				out_stream = tcpSocket.getOutputStream();	// write to the socket
				buffer_rd = new BufferedReader( new InputStreamReader( in_stream ) );
				buffer_wr = new BufferedWriter( new OutputStreamWriter( out_stream ) );
				sleep( 1000 );	// blank time
				
				// Handshake for Simulation Setting
				buffer_wr.write( Def.SIMUL_SET + System.getProperty( "line.separator" ) );
				buffer_wr.flush();
				sleep( 1000 );	// blank time
				
				String in_msg = buffer_rd.readLine();
				print_log( in_msg );
				if( !in_msg.equals( Def.SIMUL_SET_OK ) ){
					print_log( "Unknown handshaking steps. Try again.", Def.STDERR );
					
					// clean up
					buffer_wr.flush();
					buffer_rd.close();
					buffer_wr.close();
					
					// connection close
					tcpSocket.close();
					continue;
					//System.exit( 0 );
				}
				
				
				// Simulation Setting
				System.out.print( "Configuration..." );
				
				// Distributed ME type
				buffer_wr.write( "@Choose a distributed ME Scheme (1: Tokenless, 2: Token-based ): " + System.getProperty( "line.separator" ) );
				buffer_wr.flush();
				sleep( 1000 );	// blank time
				
				in_msg = buffer_rd.readLine();
				if( in_msg.equals( "1" ) ){
					ME_SCHEME = Def.PARAM_TOKENLESS;
					System.out.println( "\n[Tokenless]" );
				}else if( in_msg.equals( "2" ) ){
					ME_SCHEME = Def.PARAM_TOKEN;
					System.out.println( "\n[Token-based]" );
				}else{	// otherwise force to choose 1
					ME_SCHEME = Def.PARAM_TOKENLESS;
					System.out.println( "\n[Tokenless]" );
				}
				buffer_wr.write( ME_SCHEME + System.getProperty( "line.separator" ) );
				buffer_wr.flush();
				sleep( 1000 );	// blank time
				
				// node distribution (NOT load distribution)
				N_LOCAL_NODES = ( int )Math.floor( Conf.N_GLOBAL_NODES * Conf.N_NODES_SERVER_PORTION );
				int n_client_nodes = Conf.N_GLOBAL_NODES - N_LOCAL_NODES;
				
				buffer_wr.write( "" + n_client_nodes + System.getProperty( "line.separator" ) );
				buffer_wr.flush();
				sleep( 1000 );	// blank time
				
				in_msg = buffer_rd.readLine();
				if( in_msg.equals( Def.SIMUL_READY ) ){
					
					// network.start();	// -- create a network (which has a routing table and methods). pass the connection to the network.
					
					// running
					if( ME_SCHEME.equals( Def.PARAM_TOKENLESS ) ){
						// Ricart-Agrawala's algorithm
						System.out.println( "[Simulation] starts: Tokenless (Ricart-Agrawala)..." );
						
						// init&run threads
						System.out.println( "[Init] " + N_LOCAL_NODES + " nodes." );
						
						
//						for( int i = 0; i < Config.NUMBER_OF_NODES; i++ ){
//							nodes.add( new centralized.Node( network ) );
//							for( int j = 0; j < 5000000; j++ );
//							nodes.get( i ).start();
//						}
//						
//						Runnable input_box = new centralized.MsgInjector( network );
//						Thread shell = new Thread( input_box );
//						
//						shell.start();
						
						
					}else if( ME_SCHEME.equals( Def.PARAM_TOKEN ) ){
						// Raymond's algorithm
						System.out.println( "[Simulation] starts: Token-based (Raymond)..." );
						
						// init&run threads
						System.out.println( "[Init] " + N_LOCAL_NODES + " nodes." );
						
						
						
						
					}//end-of-if( ME_SCHEME... )
				}//end-of-if( in_msg.equals( Protocol.SIMUL_READY ) )

				//System.out.println( "\nincoming msg: " + message );
				//message += System.getProperty( "line.separator" );	// '\n'
				//buffer_wr.write( message );
				
				buffer_wr.flush();
				buffer_rd.close();
				buffer_wr.close();
				
				// connection close
				tcpSocket.close();
			}catch( IOException e ){
				e.printStackTrace();
			}
		}//end-of-while( true )
	}//end-of-MeServer( int port )
	
	
	// Sleep wrapper
	public void sleep( int duration_ms ){
		try{
			Thread.currentThread().sleep( duration_ms );
		}catch( InterruptedException e ){
			e.printStackTrace();
		}
	}
	
	
	// dump wrapper
	public void print_log( String log ){
		print_log( log, Def.STDOUT );
	}
	
	public void print_log( String log, byte target ){
		if( !Conf.VERBOSE )
			return;
		
		if( target == Def.STDOUT ){
			//System.out.print( "[MSG|" + pid + ":" + Utils.ipToString( ip ) + "(" + localtime_s + ")] " );
			System.out.println( log );
		}else if( target == Def.STDERR ){
			//System.err.print( "[ERR|" + pid + ":" + Utils.ipToString( ip ) + "(" + localtime_s + ")] " );
			System.err.println( log );
		}
	}

	
	public static void main( String[] args ){
		new MeServer( Conf.PORT_NO );
	}//end-of-main
*/
}//end-of-class
